﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace AudioEffectBox
{
    public partial class MainPage : UserControl
    {
        private CaptureSource _captureSource;
        private MyAudioSink _audioSink;
        private AudioEffectGenerator _effectGenerator;
        public MainPage()
        {
            InitializeComponent();
            
        }

       

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                btnStart.Visibility = System.Windows.Visibility.Collapsed;
                _captureSource = new CaptureSource();
                _captureSource.AudioCaptureDevice.AudioFrameSize = 50;
                _captureSource.VideoCaptureDevice = null;                                
                foreach (AudioFormat _format in _captureSource.AudioCaptureDevice.SupportedFormats)
                {                    
                    if ((_format.BitsPerSample == 16) && (_format.Channels == 1) && (_format.SamplesPerSecond == 22050))
                    {
                        _captureSource.AudioCaptureDevice.DesiredFormat = _format;                        
                    }
                }
                _effectGenerator = new AudioEffectGenerator();
                AudioGeneratorStreamSource _streamSource = new AudioGeneratorStreamSource(_effectGenerator.Output);
                _audioSink = new MyAudioSink(_effectGenerator.Input);
                _audioSink.CaptureSource = _captureSource;
                medSound.SetSource(_streamSource);
                medSound.Play();
                _captureSource.Start();
                ctlEffect.DataContext = _effectGenerator;
            }
        }
    }
}
